package com.sanpu.kxe.rxservice.client;

import android.support.v4.util.Pair;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanpu.kxe.business.member.bean.MemberBannerOutput;
import com.sanpu.kxe.business.member.bean.MemberDetailsOutput;
import com.sanpu.kxe.business.member.bean.MemberLevelOutput;
import com.sanpu.kxe.business.member.bean.MemberPayTypeOutput;
import com.sanpu.kxe.business.member.bean.MemberPaymentOutput;
import com.sanpu.kxe.business.member.bean.MemberProductRateOutput;
import com.sanpu.kxe.business.member.bean.MemberPromotOutput;
import com.sanpu.kxe.business.member.bean.MemberSpreadOutput;
import com.sanpu.kxe.business.member.bean.MemberTeamNumberOutput;
import com.sanpu.kxe.business.member.bean.MemberTeamOutput;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.rxservice.BaseClient;
import com.sanpu.kxe.rxservice.MetaBean;
import com.sanpu.kxe.rxservice.RequestMap;
import com.sanpu.kxe.rxservice.interfaces.MemberService;

import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Func1;

/**
 * Description：
 * Created by 薛勇军 on 2018/7/30.
 */

public class MemberClient extends BaseClient {

    private static MemberClient mInstance;

    public static MemberClient getInstance() {
        if (mInstance == null) {
            synchronized (MemberClient.class) {
                if (mInstance == null) {
                    mInstance = new MemberClient();
                }
            }
        }
        return mInstance;
    }

    private MemberService service;

    private MemberClient() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.getBaseUrl())
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(MemberService.class);
    }

    /**
     * 会员等级列表
     */
    public Observable<List<MemberLevelOutput>> memberLevelList() {
        return callMapAfterLogin(service.memberLevelList(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MemberLevelOutput>>>() {
                    @Override
                    public Observable<List<MemberLevelOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MemberLevelOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MemberLevelOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 会员升级广告栏
     */
    public Observable<List<MemberBannerOutput>> memberUpgrade() {
        return callMapAfterLogin(service.memberUpgrade(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MemberBannerOutput>>>() {
                    @Override
                    public Observable<List<MemberBannerOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MemberBannerOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MemberBannerOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 会员等级详情
     */
    public Observable<MemberDetailsOutput> memberLevelDetail() {
        return callMapAfterLogin(service.memberLevelDetail(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<MemberDetailsOutput>>() {
                    @Override
                    public Observable<MemberDetailsOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        MemberDetailsOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<MemberDetailsOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 会员充值方式
     */
    public Observable<MemberPaymentOutput> paymentDictinary() {
        return callMapAfterLogin(service.paymentDictinary(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<MemberPaymentOutput>>() {
                    @Override
                    public Observable<MemberPaymentOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        MemberPaymentOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<MemberPaymentOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 余额升级会员
     */
    public Observable<String> balanceUpgrade(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.balanceUpgrade(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 获取会员充值信息列表
     */
    public Observable<List<MemberLevelOutput>> findVipConfList() {
        return callMapAfterLogin(service.findVipConfList(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MemberLevelOutput>>>() {
                    @Override
                    public Observable<List<MemberLevelOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MemberLevelOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MemberLevelOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 获取会员推广列表
     */
    public Observable<List<MemberSpreadOutput>> getMemberSpreadList() {
        return callMapAfterLogin(service.getMemberSpreadList(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MemberSpreadOutput>>>() {
                    @Override
                    public Observable<List<MemberSpreadOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MemberSpreadOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MemberSpreadOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 充值会员方式列表
     */
    public Observable<List<MemberPayTypeOutput>> upgradeVipTypeList() {
        return callMapAfterLogin(service.upgradeVipTypeList(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MemberPayTypeOutput>>>() {
                    @Override
                    public Observable<List<MemberPayTypeOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MemberPayTypeOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MemberPayTypeOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 新版余额升级会员
     */
    public Observable<String> balanceUpgradeVip(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.balanceUpgradeVip(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 我的用户列表
     */
    public Observable<List<MemberPromotOutput>> myUserList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.myUserList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MemberPromotOutput>>>() {
                    @Override
                    public Observable<List<MemberPromotOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MemberPromotOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MemberPromotOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 搜索用户
     */
    public Observable<MemberPromotOutput> findUser(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.findUser(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<MemberPromotOutput>>() {
                    @Override
                    public Observable<MemberPromotOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        MemberPromotOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<MemberPromotOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 产品费率列表
     */
    public Observable<List<MemberProductRateOutput>> productRateList() {
        return callMapAfterLogin(service.productRateList(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MemberProductRateOutput>>>() {
                    @Override
                    public Observable<List<MemberProductRateOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MemberProductRateOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MemberProductRateOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 我的团队人数
     */
    public Observable<MemberTeamNumberOutput> myTeamNumber(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.myTeamNumber(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<MemberTeamNumberOutput>>() {
                    @Override
                    public Observable<MemberTeamNumberOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        MemberTeamNumberOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<MemberTeamNumberOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 我的团队列表
     */
    public Observable<List<MemberTeamOutput>> myTeamList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.myTeamList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MemberTeamOutput>>>() {
                    @Override
                    public Observable<List<MemberTeamOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MemberTeamOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MemberTeamOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }
}
